package com.dog.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dog.framework.core.exception.ServiceException;
import com.dog.framework.core.model.PageResult;
import com.dog.framework.core.utils.JsonUtils;
import com.dog.framework.mybatis.service.impl.BaseServiceImpl;
import com.dog.modules.system.cache.SysParamsCache;
import com.dog.modules.system.convert.SysParamsConvert;
import com.dog.modules.system.domain.SysParams;
import com.dog.modules.system.api.query.SysParamsQuery;
import com.dog.modules.system.api.vo.SysParamsVO;
import com.dog.modules.system.mapper.SysParamsMapper;
import com.dog.modules.system.service.SysParamsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuxk
 * @description 针对表【sys_params(参数管理)】的数据库操作Service实现
 * @createDate 2024-01-30 14:47:29
 */
@Service
@AllArgsConstructor
public class SysParamsServiceImpl extends BaseServiceImpl<SysParamsMapper, SysParams> implements SysParamsService {

    private final SysParamsCache sysParamsCache;

    @Override
    public PageResult<SysParamsVO> page(SysParamsQuery query) {
        IPage<SysParams> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        return new PageResult<>(SysParamsConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }

    private LambdaQueryWrapper<SysParams> getWrapper(SysParamsQuery query) {
        LambdaQueryWrapper<SysParams> wrapper = Wrappers.lambdaQuery();

        wrapper.like(StrUtil.isNotBlank(query.getParamKey()), SysParams::getParamKey, query.getParamKey());
        wrapper.eq(StrUtil.isNotBlank(query.getParamValue()), SysParams::getParamValue, query.getParamValue());
        wrapper.eq(query.getParamType() != null, SysParams::getParamType, query.getParamType());
        wrapper.orderByDesc(SysParams::getId);

        return wrapper;
    }

    @Override
    public void save(SysParamsVO vo) {
        // 判断 参数键 是否存在
        boolean exist = baseMapper.isExist(vo.getParamKey());
        if (exist) {
            throw new ServiceException("参数键已存在");
        }

        SysParams entity = SysParamsConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);

        // 保存到缓存
        sysParamsCache.save(entity.getParamKey(), entity.getParamValue());
    }

    @Override
    public void update(SysParamsVO vo) {
        SysParams entity = baseMapper.selectById(vo.getId());

        // 如果 参数键 修改过
        if (!StrUtil.equalsIgnoreCase(entity.getParamKey(), vo.getParamKey())) {
            // 判断 新参数键 是否存在
            boolean exist = baseMapper.isExist(vo.getParamKey());
            if (exist) {
                throw new ServiceException("参数键已存在");
            }

            // 删除修改前的缓存
            sysParamsCache.del(entity.getParamKey());
        }

        // 修改数据
        updateById(SysParamsConvert.INSTANCE.convert(vo));

        // 保存到缓存
        sysParamsCache.save(vo.getParamKey(), vo.getParamValue());
    }

    @Override
    public void delete(List<Long> idList) {
        // 查询列表
        List<SysParams> list = baseMapper.selectBatchIds(idList);

        // 删除数据
        removeByIds(idList);

        // 删除缓存
        String[] keys = list.stream().map(SysParams::getParamKey).toArray(String[]::new);
        sysParamsCache.del(keys);
    }

    @Override
    public String getString(String paramKey) {
        String value = sysParamsCache.get(paramKey);
        if (StrUtil.isNotBlank(value)) {
            return value;
        }

        // 如果缓存没有，则查询数据库
        SysParams entity = baseMapper.get(paramKey);
        if (entity == null) {
            throw new ServiceException("参数值不存在，paramKey：" + paramKey);
        }

        // 保存到缓存
        sysParamsCache.save(entity.getParamKey(), entity.getParamValue());

        return entity.getParamValue();
    }

    @Override
    public int getInt(String paramKey) {
        String value = getString(paramKey);

        return Integer.parseInt(value);
    }

    @Override
    public boolean getBoolean(String paramKey) {
        String value = getString(paramKey);

        return Boolean.parseBoolean(value);
    }

    @Override
    public <T> T getJSONObject(String paramKey, Class<T> valueType) {
        String value = getString(paramKey);
        return JsonUtils.parseObject(value, valueType);
    }
}




